package com.vs.meepet.platform

import android.content.Context
import androidx.annotation.NonNull
import com.vs.meepet.bean.SendSMSAfterCheckInfo
import com.vs.meepet.helper.GeeTestHelper
import com.vs.meepet.manager.GlobalActivityManager
import com.vs.meepet.network.GsonUtil
import com.vs.meepet.utils.LogUtils
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

/**
 *
 * 作者：柯嘉少
 * 邮箱：2449926649@qq.com
 * 日期: 2024/9/19
 * 修改人: xxx(des)
 * 说明:
 * 版本: 1.0
 */
class GeeTestPlugin : FlutterPlugin {
    private var context: Context? = null
    private var geeTestHelper: GeeTestHelper? = null

    override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        val channel = MethodChannel(binding.binaryMessenger, "geeTest")
        channel.setMethodCallHandler(this::onMethodCall)
        context = GlobalActivityManager.INSTANCE.currentActivity
        geeTestHelper = GeeTestHelper()
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        geeTestHelper?.destroy()
        geeTestHelper = null
        context = null
    }

    private fun onMethodCall(@NonNull call: MethodCall, @NonNull result: MethodChannel.Result) {
        when (call.method) {
            "sendSMSAfterCheck" -> {
                LogUtils.i("成功调用了插件：sendSMSAfterCheck")
                LogUtils.i("插件传入的参数是：" + call.arguments.toString())
                try {
                    var bean = GsonUtil.gson.fromJson<SendSMSAfterCheckInfo>(
                        call.arguments.toString(),
                        SendSMSAfterCheckInfo::class.java
                    )
                    LogUtils.i("解析后的数据是：" + bean.toString())

                    geeTestHelper?.show(
                        context,
                        bean.phone.orEmpty(),
                        bean.geekRegisterData
                    ) { isSuccess, msg ->
                        if (isSuccess) {
                            result.success("0")
                            //result.error("-1（失败）", "失败", null)
                        } else {
                            result.success("-1")
                        }
                    }

                } catch (e: Exception) {
                    result.success("-1")
                }
                LogUtils.i("插件传入的参数是：" + call.arguments)
            }

            else -> {
                result.notImplemented()
            }
        }
    }
}